/**
 * 
 */
package com.yaodian100.core.admin.web;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;

import com.yaodian100.core.admin.entity.Group;
import com.yaodian100.core.admin.service.GroupService;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * @author jeffma
 * 
 */
public class GroupAction extends AbstractAction {

	@Resource
	private GroupService groupService;

	private Group obj;

	private List<Group> objList;

	private String paraId;
	private String paraKey;
	private String paraDesc;

	private List<String> paraTypes;
	private Collection typeMenu;

	/**
	 * @return the paraId
	 */
	public String getParaId() {
		return paraId;
	}

	/**
	 * @param paraId the paraId to set
	 */
	public void setParaId(String paraId) {
		this.paraId = paraId;
	}

	/**
	 * @return the paraKey
	 */
	public String getParaKey() {
		return paraKey;
	}

	/**
	 * @param paraKey the paraKey to set
	 */
	public void setParaKey(String paraKey) {
		this.paraKey = paraKey;
	}

	/**
	 * @return the paraDesc
	 */
	public String getParaDesc() {
		return paraDesc;
	}

	/**
	 * @param paraDesc the paraDesc to set
	 */
	public void setParaDesc(String paraDesc) {
		this.paraDesc = paraDesc;
	}

	/**
	 * @return the paraTypes
	 */
	public List<String> getParaTypes() {
		return paraTypes;
	}

	/**
	 * @param paraTypes the paraTypes to set
	 */
	public void setParaTypes(List<String> paraTypes) {
		this.paraTypes = paraTypes;
	}

	/**
	 * @return the obj
	 */
	public Group getObj() {
		return obj;
	}

	/**
	 * @return the objList
	 */
	public List<Group> getObjList() {
		return objList;
	}

	/**
	 * @return the typeMenu
	 */
	public Collection getTypeMenu() {
		return typeMenu;
	}

	public String search() {
		CommonCriteria cri = new CommonCriteria();
		if (StringUtils.isNotEmpty(paraKey)) {
			cri.addRlike("code", paraKey);
		}
		if (StringUtils.isNotEmpty(paraDesc)) {
			cri.addRlike("description", paraDesc);
		}
		if (paraTypes != null && paraTypes.size() > 0) {
			cri.addIn("type", paraTypes);
		}

		try {
			int entitySize = groupService.getListSize(cri).intValue();
			getPager().setTotalSize(entitySize);

			objList = groupService.getList(getPager().getCurrentPage() * getPager().getPageRecord(), getPager()
					.getPageRecord(), cri, new String[] { "code asc" });

			logger.info("pager:{},groups:{}", getPager(), objList);
		} catch (CoreException e) {
			addActionError(e.getMessage());
			e.printStackTrace();
		}
		return "list";
	}

	public String view() {
		try {
			if (StringUtils.isNotBlank(paraId)) {
				obj = groupService.get(paraId);
				if (obj == null) {
					addActionError("Group not found");
					return "list";
				}
			} else {
				addActionError("Group ID not found");
				return "list";
			}
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "list";
		}
		return "view";
	}

	public String create() {
		obj = new Group();
		return "input";
	}

	public String save() {
		if (obj == null) {
			addActionError("Group not found");
			return "list";
		}

		if (obj.getModifyDate() == null) {
			// 因為重新建立的 entity 才會 re-new name
			obj = new Group(obj.getCode().toUpperCase(), obj.getDescription());
		}
		try {
			groupService.save(obj);
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "input";
		}
		return "view";
	}

	public String edit() {
		return "input";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.common.web.AbstractAction#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		if (typeMenu == null) {
			Map m = new HashMap();
			m.put("Y", "耀点");
			m.put("C", "客服");
			m.put("S", "供应商");
			typeMenu = m.entrySet();
		}

		if (paraTypes == null) {
			paraTypes = Arrays.asList("Y", "C", "S");
		}
		super.prepare();
	}
}
